<?php

namespace App\Admin\Controllers\Family;

use App\Models\Family;
use App\Models\Relation;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\MessageBag;

class UserController extends Controller
{
    use ModelForm;

    public $header = '家庭成员列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form1()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Family\User::class, function (Grid $grid) {
            if (Request::get('fid')){
                $grid->model()->where('family_id', Request::get('fid'));
            }
            $grid->id('编号')->sortable();
            $grid->family()->number('户号');
            $grid->family()->address('地址');
            $grid->user()->username('用户名称');
            $grid->is_householder('身份')->using([
                '1' => '户主'
            ])->label();


            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

            /*$grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('name', '名称');
                $filter->between('created_at', '添加时间')->datetime();
            });*/

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Family\User::class, function (Form $form) {

            $family_id = Request::get('fid');

            $form->display('id', '编号');

            $form->select('family_id','家庭户号')->options(
                Family::all()->pluck('number', 'id')


            )->setWidth(4)->default($family_id);

            $form->select('user_id','用户')->options(
                User::whereNotIn('id', function ($query){
                    $query->select('user_id')
                        ->from('love_family_users')
                        ->get();
                })->pluck('username', 'id')
            )->setWidth(4);

            $form->select('relation_id','与户主关系')->options(
                Relation::all()->pluck('name', 'id')
            )->setWidth(4)->rules('required');


            $form->disableViewCheck();

            $form->saving(function (Form $form){

                $user_id = $form->user_id;
                $family_id = $form->family_id;

                $user = Family\User::where('user_id', $user_id)->first();
                if ($user && $family_id != $user->family_id){
                    $error = new MessageBag([
                        'title'   => '添加失败',
                        'message' => '该人员已经在其他家庭里存在',
                    ]);

                    return back()->with(compact('error'));
                }

            });

            /*$form->saved(function (Form $form){

                $user = new Family\User;
                $user->family_id = $form->model()->id;
                $user->user_id = $form->model()->householder_id;
                $user->is_householder = 1;

                $user->save();

            });*/
        });
    }

    protected function form1()
    {
        return Admin::form(Family\User::class, function (Form $form) {

            $family_id = Request::get('fid');

            $form->display('id', '编号');

            $form->select('family_id','家庭户号')->options(
                Family::all()->pluck('number', 'id')


            )->setWidth(4)->default($family_id);

            $form->select('user_id','用户')->options(
                User::all()->pluck('username', 'id')
            )->setWidth(4);

            $form->select('relation_id','与户主关系')->options(
                Relation::all()->pluck('name', 'id')
            )->setWidth(4)->rules('required');


            $form->disableViewCheck();

            $form->saving(function (Form $form){

                dd(111);
                $user_id = $form->user_id;
                $family_id = $form->family_id;
                $user = Family\User::where('user_id', $user_id)->first();
                if ($user){
                    $error = new MessageBag([
                        'title'   => '添加失败',
                        'message' => '该人员已经在其他家庭里存在',
                    ]);

                    return back()->with(compact('error'));
                }

            });

            /*$form->saved(function (Form $form){

                $user = new Family\User;
                $user->family_id = $form->model()->id;
                $user->user_id = $form->model()->householder_id;
                $user->is_householder = 1;

                $user->save();

            });*/
        });
    }



}
